﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;

namespace Medianamik.UI.Controls
{
    public class NodeActivationButton : SecuredImageButton
    {
        public bool IsActive
        {
            get
            {
                return (bool?)ViewState["IsActive"] ?? true;
            }
            set
            {
                ViewState["IsActive"] = value;
            }
        }

        public override string ImageUrl
        {
            get
            {
                if(Enabled)
                    return IsActive ? "/medianamik/images/Icones/Ico_Actif.png" : "/medianamik/images/Icones/Ico_Inactif.png";

               return base.ImageUrl;
            }
            set
            {
                throw new InvalidOperationException("Cannot set ImageUrl for this control");
            }
        }
        
        public string ActivateCommandName
        {
            get
            {
                return ViewState["ActivateCommandName"] as string
                    ?? "Activate";
            }
        }

        public string DeactivateCommandName
        {
            get
            {
                return ViewState["DeactivateCommandName"] as string
                    ?? "Deactivate";
            }
        }

        public string ActivateMessage
        {
            get
            {
                return ViewState["ActivateMessage"] as string
                    ?? (string)HttpContext.GetGlobalResourceObject("Medianamik_Global", "NodeActivationButton_ActivateMessage"); 
            }
            set
            {
                ViewState["ActivateMessage"] = value;
            }
        }

        public string DeactivateMessage
        {
            get
            {
                return ViewState["DeactivateMessage"] as string 
                    ?? (string)HttpContext.GetGlobalResourceObject("Medianamik_Global", "NodeActivationButton_DeactivateMessage"); 
            }
            set
            {
                ViewState["DeactivateMessage"] = value;
            }
        }
        
        public string Message
        {
            get
            {
                return IsActive ? DeactivateMessage : ActivateMessage;
            }
        }

        public override string ToolTip
        {
            get
            {
                return ViewState["ToolTip"] as string 
                    ?? (string)HttpContext.GetGlobalResourceObject("Medianamik_Global", "NodeActivationButton_Tooltip");  
            }
            set
            {
                ViewState["ToolTip"] = value;
            }
        }

        public override string AlternateText
        {
            get
            {
                return ToolTip;
            }
            set
            {
                ToolTip = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CommandName = IsActive ? DeactivateCommandName : ActivateCommandName;
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if (Enabled)
            {
                writer.AddAttribute("onclick", String.Format("radconfirm('{0}', function(arg) {{ if(arg==true) {{ " + Page.ClientScript.GetPostBackEventReference(this, string.Empty) + "; }} }} ); return false;", Message));
            }
            writer.AddAttribute("title", AlternateText);

            base.RenderBeginTag(writer);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Enabled)
            {
                base.Render(writer);
            }
            else
            {
                //Tout simplement afficher une image
                writer.Write("<img src=\"{0}\" alt=\"{1}\" class=\"{2}\" />",
                    DisabledImageUrl, AlternateText, CssClass);
            }
        }

        public virtual string DisabledImageUrl
        {
            get
            {
                return ViewState["DisabledImageUrl"] as string ?? string.Empty;
            }
            set
            {
                ViewState["DisabledImageUrl"] = value;
            }
        }
    }
}
